#!/usr/bin/perl


$DEBUG= 0;
$LOGGING= 1;
$MAX_DEPTH= 2;
$BACKUP_DIR= "/wwcom/dumps";

select(STDERR);
$|= 1;
select(STDOUT);
$|= 1;


if ($ARGV[0]!~ /^(\d+)?$/) {
    print "Usage: backup-wwcom.pl <timeout (secs)>\n";
    exit(1);
}
$pause= $ARGV[0];
my $cont= 1;
while ($cont)
{
    $cont= 0 if (!$pause);

    @tm= localtime();
    $tm[5]+= 1900;
    $tm[4]= sprintf("%02d",$tm[4]+1);
    $tm[3]= sprintf("%02d",$tm[3]);
    $tm[2]= sprintf("%02d",$tm[2]);
    $tm[1]= sprintf("%02d",$tm[1]);
    $code= "$tm[5]$tm[4]$tm[3]-$tm[2]$tm[1]";

    print "Backing up WWCOM on localhost to wwcom.dump.$code.sql ...";
    chdir($BACKUP_DIR);
    open(DUMP,"mysqldump --user=wwcom --password=wwcom --opt wwcom |");
    open(FH,">wwcom.dump.$code.sql");
    foreach $line (<DUMP>) {
        print FH $line;
    }
    close (DUMP);
    close (FH);

    print "done\n";


    if ($cont) {
        @tm2= localtime(time+$pause);
        $tm2= sprintf("%02d",$tm2[3]);
        $tm2[2]= sprintf("%02d",$tm2[2]);
        $tm2[1]= sprintf("%02d",$tm2[1]);
        print "Paused for $pause seconds - until $tm2[2]:$tm2[1]:$tm2[0]\n";
        sleep $pause;
    }
}

#####################################################################
###########                    Subroutines                  #########
#####################################################################
